const fs = require('fs');
//引入multer
const multer = require('multer');
//创建磁盘存储对象 （多文件）
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        //先定义一个动态目录
        const date = new Date();
        let s = 'public/upload/';
        s += date.getFullYear() + '/';
        s += (date.getMonth() + 1) + '/';
        s += (date.getDate()) + '/';
        //mkdirSync同步创建 recursive允许一次创建多个目录
        fs.mkdirSync(s, { recursive: true })
        cb(null, s)
    },

    filename: function (req, file, cb) {
        //文件名的指定要求文件名越长越好，避免重复 
        const date = new Date();
        let s = '';
        s += date.getFullYear();
        s += ('0' + (date.getMonth() + 1)).slice(-2);
        s += ('0' + (date.getHours() + 1)).slice(-2);
        s += ('0' + (date.getMinutes() + 1)).slice(-2);
        s += ('0' + (date.getSeconds() + 1)).slice(-2);
        s += ('00' + (date.getMilliseconds())).slice(-3);
        const arr = file.originalname.split('.');
        s += '.' + arr[arr.length - 1];
        cb(null, s);
    }
})

//创建上传存储的实例对象
const upload = multer({
    storage,

})
module.exports = upload